﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering.View_Operaction;
using Catering_DBOpertion;

namespace Catering.yl.Daily
{
    public partial class DynamicPM : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {   
            if(!Page.IsPostBack)
            {
                LoadDrop();
                LoadForm();
            }            
        }
        protected void LoadDrop()
        {
            string userid = Session["PersonnelID"].ToString();
            int companyid = 0;
            int groupid = 0;
            int classid = 0;
            bool isAdmin = false;
            Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
            isAdmin = true;
            Bind_Data_to_Controls.Bind_Area_ddl(Drop_ZuoYeQu, isAdmin, groupid, companyid, false);
            Bind_Data_to_Controls.Bind_Class_ddl(Drop_ZuoYeQu.SelectedItem, Drop_BanZu, isAdmin, classid, false);

            if (Session["RBGropuID"] != null) this.Drop_ZuoYeQu.SelectedValue = Session["RBGropuID"].ToString();
            if (Session["RBClassID"] != null) this.Drop_BanZu.SelectedValue = Session["RBClassID"].ToString();
            if (!isAdmin)
            {
                this.Drop_ZuoYeQu.Enabled = false;
                this.Drop_BanZu.Enabled = false;
            }
            
            Session.Remove("RBGropuID");
            Session.Remove("RBClassID");
            Session.Remove("RBDate");
        }
        public DataSet SelMembersByCID(string strWhere, string str1)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,CName");
            strSql.Append(" FROM ContractorInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where CID =" + int.Parse(strWhere) + str1);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        protected void SelMember_Click(object sender, EventArgs e)
        {
            CBL_ConMembers.Items.Clear();
            if (Session["ContractorRID"] != null)
            {
                DataSet ds = SelMembersByCID(Session["ContractorRID"].ToString(), " and ClassID = '' and GroupID = ''");
                DataTable dt = ds.Tables[0];
                CBL_ConMembers.DataSource = dt;
                CBL_ConMembers.DataValueField = "id";
                CBL_ConMembers.DataTextField = "CName";
                CBL_ConMembers.DataBind();
            }
            mainOpts.Visible = false;
            mainMember.Visible = true;
        }
        protected void LoadForm()
        {
            MemList.Items.Clear();
            if (Session["ContractorRID"] != null)
            {
                DataSet ds = SelMembersByCID(Session["ContractorRID"].ToString(), " and ClassID ='" + Session["ClassID"].ToString() + "' and GroupID ='" + Session["GroupID"].ToString() + "'");
                DataTable dt = ds.Tables[0];
                MemList.DataSource = dt;
                MemList.DataValueField = "id";
                MemList.DataTextField = "CName";
                MemList.DataBind();
            }
        }

        protected void ChangeClass_Click(object sender, EventArgs e)
        {
            if (MemList.SelectedValue.ToString() == null ||MemList.SelectedValue.ToString() == "")
            {
                string strOpenUrl = "alert('请选择相应人员!')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
                return;
            }
           Edit(int.Parse(MemList.SelectedValue));
           string strResult = "alert('转班组成功')";
           ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strResult, true);
        }        
        public DataSet SelMemberByCID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 GroupID,ClassID");
            strSql.Append(" FROM ContractorInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id=" + int.Parse(strWhere));
            }
            return DbHelperSQL.Query(strSql.ToString());
        }     
        public void Edit(int id)
        {
            Catering_Model.ContratorMember _cm = new Catering_Model.ContratorMember();
            _cm.Id = id;
            _cm.ClassID = Drop_BanZu.SelectedValue;
            _cm.GroupID = Drop_ZuoYeQu.SelectedValue;
            DataSet ds = SelMemberByCID(id.ToString());
            DataTable dt = ds.Tables[0];
            if (dt.Rows[0]["GroupID"] != null && dt.Rows[0]["ClassID"] != null && dt.Rows[0]["ClassID"].ToString() == "" && dt.Rows[0]["GroupID"].ToString()!="")
            {
                _cm.OldClassID = dt.Rows[0]["ClassID"].ToString();
                _cm.OldGroupID = dt.Rows[0]["GroupID"].ToString();
            }
            else
            {
                _cm.OldClassID = Session["ClassID"].ToString();
                _cm.OldGroupID = Session["GroupID"].ToString();
            }
            _cm.ChangeClassDate = DateTime.Today;
            new Catering_Rules.ContratorMember().Update(_cm, "");            
        }

        protected void Drop_ZuoYeQu_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt_BanZu = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_ZuoYeQu.SelectedValue).Tables[0];
            this.DropBind(this.Drop_BanZu, dt_BanZu, "id", "CName");
        }

        protected void Drop_BanZu_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DataTable dt_BanZu = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.Drop_ZuoYeQu.SelectedValue).Tables[0];
            //this.DropBind(this.Drop_BanZu, dt_BanZu, "id", "CName");            
        }
        //公用的DropDownList绑定方法
        public void DropBind(DropDownList drop, DataTable dt, string strValue, string strText)
        {
            drop.Items.Clear();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = dt.Rows[i][strText].ToString();
                    item.Value = dt.Rows[i][strValue].ToString();
                    drop.Items.Add(item);
                }
            }
        }

        protected void return_null_Click(object sender, EventArgs e)
        {
            if (MemList.SelectedValue.ToString() == null || MemList.SelectedValue.ToString() == "")
            {
                string strOpenUrl = "alert('请选择相应人员!')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
                return;
            }
                
            Catering_Model.ContratorMember _cm = new Catering_Model.ContratorMember();
            _cm.Id = int.Parse(MemList.SelectedValue);
            _cm.ClassID = "";
            _cm.GroupID = "";           
            _cm.ChangeClassDate = DateTime.Today;
            if (new Catering_Rules.ContratorMember().Update(_cm, "", ""))
            {
                string strResult = "alert('返陆地成功')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strResult, true);
            }
            MemList.Items.Clear();
            LoadForm();
        }

        protected void sel_selMember_Click(object sender, EventArgs e)
        {           
            foreach(ListItem li in CBL_ConMembers.Items)
            {
                if( li.Selected )
                {
                    Edit(int.Parse(li.Value));                   
                }
            }
            MemList.Items.Clear();
            LoadForm();
            mainOpts.Visible = true;
            mainMember.Visible = false;
        }       
    }
}